/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.yoyo.fragment.reading;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.yoyo.adapter.ReadDataRecyclerAdapter;
import com.xuexiang.yoyo.constant.HesAppStatic;
import com.xuexiang.yoyo.core.BaseFragment;
import com.xuexiang.yoyo.databinding.FragmentReadingBinding;
import com.xuexiang.yoyo.utils.MeterUtil;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

/**
 * @author liuxiangfei
 * @since 2023-07-24 00:19
 */
@Page(anim = CoreAnim.none)
public class ReadingFragment extends BaseFragment<FragmentReadingBinding> {

    private ReadDataRecyclerAdapter mAdapter;
    @NonNull
    @Override
    protected FragmentReadingBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentReadingBinding.inflate(inflater, container, attachToRoot);
    }

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle().setTitle("学习");
        titleBar.addAction(
                new TitleBar.TextAction("设置"){

                    @Override
                    public void performAction(View view) {
                        openNewPage(ReadingTestFragment.class);
                    }
                }
        );
        return titleBar;
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initViews() {
        binding.etMeterCoder.setText(HesAppStatic.getReadingDeviceId());
        initNotConnectView();

        initReadBt();

        binding.ScrollView01.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                binding.ScrollView01.post(new Runnable() {
                    public void run() {
                        binding.ScrollView01.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }
        });


        WidgetUtils.initRecyclerView(binding.recyclerView);
        binding.recyclerView.setAdapter(mAdapter = new ReadDataRecyclerAdapter());
        mAdapter.setOnItemClickListener((itemView, item, position) -> {
            XToastUtils.toast(item.getDataItemName()+"\t"+item.getLn());
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    private void initReadBt() {
        binding.readBt.setOnClickListener(v -> {
            if(binding.etMeterCoder.isEnabled()){
                binding.etMeterCoder.setEnabled(!binding.etMeterCoder.isEnabled());
                binding.readBt.setText(binding.etMeterCoder.isEnabled() ? "Read" : "Reading");
                binding.notConnectView.setVisibility(View.VISIBLE);
                HesAppStatic.setReadingDeviceId(binding.etMeterCoder.getText().toString());
                mAdapter.clear();
                readData();
            }
            //loadData();
        });
    }

    private int readDeviceCount = -1;
    private void readData() {
        if(HesAppStatic.isPressTestFlag()){
            if(readDeviceCount<=0){
                readDeviceCount = Integer.valueOf(HesAppStatic.getPressTestNum());
            }
            HesAppStatic.setReadingDeviceCodeVirtually(MeterUtil.createDeviceCodeVirtually(readDeviceCount--));
            binding.etMeterCoder.setText(HesAppStatic.getReadingDeviceCodeVirtually());
        }else{
            HesAppStatic.setReadingDeviceCodeVirtually(HesAppStatic.getReadingDeviceId());
        }

    }

    private void initNotConnectView() {
        binding.notConnectView.setVisibility(View.GONE);
        int size = DensityUtils.dp2px(getContext(), 20);
        XUIGroupListView.newSection(getContext()).setTitle("学习数据")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addTo(binding.notConnectView);
    }

}
